package com.luoxiang.socialandroid.activity;

import static com.luoxiang.socialandroid.common.Constants.MmkvConst.ACCID;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;


import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.luoxiang.socialandroid.R;
import com.luoxiang.socialandroid.base.BaseActivity;
import com.luoxiang.socialandroid.utils.CustomClickListener;
import com.luoxiang.socialandroid.utils.MmkvUtil;
import com.luoxiang.socialandroid.utils.SaveImageUtils;

import java.io.FileNotFoundException;
import java.io.OutputStream;

public class ShareActivity extends BaseActivity {
    private ImageView img_erweimaUrl;
    private Button btn_baocun, icon_back;
    private RelativeLayout relaImageerweima;

    @Override
    protected void initListener() {
        btn_baocun.setOnClickListener(new CustomClickListener() {
            @Override
            protected void onSingleClick() {
                Bitmap bitmap = convertViewToBitmap(relaImageerweima);
                saveImage29(bitmap);
            }

            @Override
            protected void onFastClick() {

            }
        });
        icon_back.setOnClickListener(new CustomClickListener() {
            @Override
            protected void onSingleClick() {
                finish();
            }

            @Override
            protected void onFastClick() {

            }
        });
    }

    /**
     * API29 中的最新保存图片到相册的方法
     */
    private void saveImage29(Bitmap toBitmap) {
        //开始一个新的进程执行保存图片的操作
        Uri insertUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new ContentValues());
        //使用use可以自动关闭流
        try {
            OutputStream outputStream = getContentResolver().openOutputStream(insertUri, "rw");
            if (toBitmap.compress(Bitmap.CompressFormat.JPEG, 90, outputStream)) {
                Log.e("保存成功", "success");
                ToastUtils.showShort(R.string.mi_yibaocundaoxiangce);

            } else {
                Log.e("保存失败", "fail");
                ToastUtils.showShort("图片保存失败");
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    public Bitmap convertViewToBitmap(View view) {
        Bitmap bitmap = Bitmap.createBitmap(1080, 1920, Bitmap.Config.ARGB_8888);
        //利用bitmap生成画布
        Canvas canvas = new Canvas(bitmap);
        //把view中的内容绘制在画布上
        view.draw(canvas);
        return bitmap;
    }

    @Override
    protected void initLayout(Bundle savedInstanceState) {
        setContentView(R.layout.activity_share);
        setStatusBarTranslucent();
    }

    @Override
    protected void initView() {
        img_erweimaUrl = findViewById(R.id.img_erweimaUrl);
        btn_baocun = findViewById(R.id.btn_baocun);
        relaImageerweima = findViewById(R.id.relaImageerweima);
        icon_back = findViewById(R.id.icon_back);
        Intent intent = getIntent();
        String erweimaUrl = intent.getStringExtra("erweimaUrl");
        Glide.with(ShareActivity.this).load(erweimaUrl).into(img_erweimaUrl);
        ((TextView) findViewById(R.id.textUserId)).setText("ID " + MmkvUtil.INSTANCE.decodeInt(ACCID));
    }
}